import Vue from "vue";
import moment from "moment/moment";
import * as util from "@/common/util";

/**
 * 语言格式化
 * @param permissions
 * @returns {{}}
 */
export const langFormat = (val) => {
    return util.langFormat(val)
}


// 字符串格式化
export const strFormat = (val) => {
    let res
    if (val == null || val == '') {
        res = '--'
        return res
    } else {
        res = val
        return res
    }
}

// 数字格式化
export const numberFormat = (val) => {
    if (val === '' || val === null || val === undefined || val === 0) {
        return 0
    } else {
        return val
    }
}

// 金额格式化
export const amountFormat = (amount, addAndSubSign = false) => {
    let res = ''
    let symbol = Vue.prototype.$currency.symbol
    if (addAndSubSign && (!amount || amount === 0)) {
        return '--'
    }
    if (amount) {
        if (addAndSubSign && amount > 0) {
            res += '+'
        }
        res += Vue.prototype.$util.formatAmount(amount)
        res = symbol + res
    } else {
        res = `${symbol}0.00`
    }
    // console.log('amountFormat= (amount)
    return res
}

// 状态格式化
export const stateFormat = (val, list) => {
    if (val === null || val === '') {
        return ''
    }
    const find = (list || []).find(item => {
        if (item.value === null || item.value === '') {
            return false
        }
        return String(item.value) === String(val)
    })
    return find ? find.label : ''
}

// 百分比格式化
export const ratioFormat = (val, total) => {
    const res = util.calcPercentage(val, total)
    return res === 0 ? '--' : res
}

// 日期格式化
export const dateformat = (val, pattern = Vue.prototype.$dateTimePattern) => {
    return val ? moment(val).format(pattern) : '--'
}


// 日期范围格式化
export const dateRangeFormat = (start, end, pattern = Vue.prototype.$datePattern) => {
    if (start && end) {
        return `${moment(start).format(pattern)} ~ ${moment(end).format(pattern)}`
    } else {
        return ''
    }
}


// 手机号格式化
export const phoneFormat = (val, code = '') => {
    if (val === null || val === '' || val === undefined) {
        return '--'
    }
    // (+86) 13809431234
    const s = `${code ? `(+${code}) ` : ''} ${val}`
    // console.log(`code: ${code}, val: ${val}, result: ${s}`)
    return s
}



//JSON时间格式化
export const timeFormats = vals => {
    let val
    if (vals == null || vals == '') {
        val = '--'
        return val
    } else {
        val = JSON.parse(vals)
        if (val.day) {
            return val.day + (Vue.prototype.$cache.getLang() == 'CN' ? '' : ` `) + '天'
        } else if (val.hour) {
            return val.hour + (Vue.prototype.$cache.getLang() == 'CN' ? '' : ` `) + '小时'
        } else if (val.minute) {
            return val.minute + (Vue.prototype.$cache.getLang() == 'CN' ? '' : ` `) + '分钟'
        } else if (!val.day && !val.hour && !val.minute) {
            val = '--'
            return val
        }
    }
}


// uid格式化
export const uidFormat = (val) => {
    const arr = ('' + val).split('')
    const diff = arr.length - 8
    if (diff < 0) {
        return (
            `U` +
            Array(0 - diff)
            .fill(0, 0, 0 - diff + 1)
            .concat(arr)
            .join('')
        )
    } else {
        return `u` + arr.join('')
    }
}

export function padZero(num) {
    if (num < 10) {
        return '0' + num
    } else {
        return num
    }
}


// 秒转时间-天、时、分、秒
export function formatSeconds(value) {
    if (!value) {
        return ''
    }
    const duration = moment.duration(parseInt(value), 'seconds');
    let days = Math.floor(duration.asDays()) > 0 ? padZero(Math.floor(duration.asDays())) + Vue.prototype.$lang.Date_Day : '';
    let hours = padZero(duration.hours()) + Vue.prototype.$lang.Date_Hrs;
    let minutes = padZero(duration.minutes()) + Vue.prototype.$lang.Date_Mins;
    let seconds = padZero(duration.seconds()) + Vue.prototype.$lang.Date_Secs;
    return `${days}${hours}${minutes}${seconds}`
}

// 字节转MB
export function formatBytes(value) {
    let res = (value / 1024 / 1024).toFixed(2) + ' MB'
    return res
}

// 字节转MB/KB
export function formatBytest(value) {
    if (value < 1048576) {
        let res = (value / 1024).toFixed(2) + ' KB'
        return res
    } else {
        let res = (value / 1024 / 1024).toFixed(2) + ' MB'
        return res
    }
}

//信号值格式化
export function signalFormat(value) {
    if (!value) return ''
    if (Number(value) < 0) {
        if (value > -40) {
            return Vue.prototype.$lang.Strong + '(' + value + ')'
        } else if (value < -85) {
            return Vue.prototype.$lang.Weak + '(' + value + ')'
        } else {
            return Vue.prototype.$lang.Users_Status_Dropdown_Normal + '(' + value + ')'
        }
    } else {
        if (value > 24) {
            return Vue.prototype.$lang.Strong + '(' + value + ')'
        } else if (value < 15) {
            return Vue.prototype.$lang.Weak + '(' + value + ')'
        } else {
            return Vue.prototype.$lang.Users_Status_Dropdown_Normal + '(' + value + ')'
        }
    }
}